$(function(){
    $('#link-reg').on('click',function(){
        $('.login-box').hide()
        $('.reg-box').show()
    })
    $('#link-login').on('click',function(){
        $('.login-box').show()
        $('.reg-box').hide()
    })

    let form = layui.form
    form.verify({
        username:[
            /^[a-zA-Z0-9]{1,10}$/,
            '用户名必须是1-10位字母和数字'
        ],
        pwd:[
            /^[\S]{6,15}$/,
            '密码长度必须是6-15位的非空字符串'
        ],
        rePwd: function (value) {
            let pwd = $('.rebox [name=password]').val()
            if(pwd !== value){
                return '两次输入密码不值一致！'
            }
        }
    })

    let layer = layui.layer
    $('#formReg').on('submit',function(e){
        e.preventDefault()
        axios({
            method: 'POST',
            url: '/api/reguser',
            data: $(this).serialize()
        }).then(({ data:res }) => {
            if(res.status !== 0){
                return layer.msg(res.message)
            }
            layer.msg('注册用户成功！')
            $('#link-login').click()
            $('#formReg')[0].reset()
        })
    })

    $('#formLogin').on('submit',function(e){
        e.preventDefault()
        axios({
            method: 'POST',
            url: '/api/login',
            data: $(this).serialize()
        }).then(({ data:res }) => {
            if(res.status !== 0){
                layer.msg(res.message)
            }
            layer.msg('登录成功！')
            location.href = '/index.html'
            localStorage.setItem('token',res.token)
        })
    })

})